﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace KTX
{
    public partial class frmChiTietDienNuoc : DevExpress.XtraEditors.XtraForm
    {
        public frmChiTietDienNuoc()
        {
            InitializeComponent();
            
        }
        Data.DienNuoc _dn;
        Data.ChiTietThang _thang;
        public frmChiTietDienNuoc(Data.DienNuoc dn,Data.QLKTXEntities _db)
            : this()
        {db = _db;
            _dn = dn;
            this.dienNuocBindingSource.DataSource = dn;
        }
        public frmChiTietDienNuoc( Data.ChiTietThang thang,Data.QLKTXEntities _db)
            : this()
        {db = _db;
            _dn = thang.DienNuoc;
            this.dienNuocBindingSource.DataSource = _dn;
            _thang = thang;
        }

        Data.QLKTXEntities db;
        private void frmChiTietDienNuoc_Load(object sender, EventArgs e)
        {
            if (this.dienNuocBindingSource.Current!=null)
            {
                this.chiTietThangBindingSource.DataSource = db.ChiTietThang.Where(p => p.MaDienNuoc == -1);
            }
            else
            {
                MessageBox.Show("Không xác đinh được phòng thu tiền điện nước");
            }
        }

        private void dienNuocBindingSource_CurrentChanged(object sender, EventArgs e)
        {
          
        }

        private void frmChiTietDienNuoc_Shown(object sender, EventArgs e)
        {
            if (this.dienNuocBindingSource.Current != null)
            {
                if (_thang!=null)
                {
                    this.chiTietThangBindingSource.DataSource = _thang;
                }
                else
                {
                    this.chiTietThangBindingSource.AddNew();
                }
                
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
        }

        private void chiTietThangBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Data.ChiTietThang()
            {
                MaDienNuoc = _dn.MaDienNuoc,DenNgay = DateTime.Now,TuNgay =DateTime.Now.AddDays(-30),
                DGDien =15000,DGNuoc =70000
            };
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.chiTietThangBindingSource.Current!=null)
            {
                var report = new BaoCao.xPhieuDienNuoc();
            BindingSource bs = (BindingSource)report.DataSource;
            bs.DataSource = chiTietThangBindingSource.Current;
            report.ShowPreviewDialog();
            }
        }
    }
}